package com.jy.adscm.operation.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyDept;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.QueryTaskWorkPlan;
import com.jy.adscm.operation.base.bean.TaskWorkPlan;
import com.jy.adscm.operation.base.bean.TaskWorkPlanDetail;
import com.jy.adscm.operation.base.mapper.mysql.TaskWorkPlanDetailMapper;
import com.jy.adscm.operation.base.mapper.mysql.TaskWorkPlanMapper;
import com.jy.adscm.operation.base.service.TaskWorkPlanService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyDeptMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 个人工作计划 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-05-26
 */
@Service
public class TaskWorkPlanServiceImpl extends ServiceImpl<TaskWorkPlanMapper, TaskWorkPlan> implements TaskWorkPlanService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    JyDeptMapper jyDeptMapper;
     @Autowired
    TaskWorkPlanDetailMapper taskWorkPlanDetailMapper;
     @Autowired
     TaskWorkPlanMapper taskWorkPlanMapper;
     @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertTaskWorkPlan(TaskWorkPlan taskWorkPlan, Token token) throws AdscmSystemException {
        taskWorkPlan.setCreator(token.getUser().getId().toString());
         taskWorkPlan.setDept(token.getUser().getDeptId());
        taskWorkPlan.setCreateTime(new Date());
         boolean insert = super.insert(taskWorkPlan);
         List<TaskWorkPlanDetail> taskWorkPlanDetails = taskWorkPlan.getTaskWorkPlanDetails();
         if (taskWorkPlanDetails!=null && taskWorkPlanDetails.size()>0){
             for (TaskWorkPlanDetail taskWorkPlanDetail : taskWorkPlanDetails) {
                 taskWorkPlanDetail.setCreator(token.getUser().getId().toString());
                 taskWorkPlanDetail.setCreateTime(new Date());
                 taskWorkPlanDetail.setPid(taskWorkPlan.getId());
                 taskWorkPlanDetailMapper.insert(taskWorkPlanDetail);
             }
         }
         return insert;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateTaskWorkPlan(TaskWorkPlan taskWorkPlan, Token token) {
        if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlan.getState(),null)){
            if (taskWorkPlan.getState()==2){
                Wrapper<TaskWorkPlanDetail> workPlanDetailWrapper=new EntityWrapper<>();
                workPlanDetailWrapper.eq("pid",taskWorkPlan.getId());
                List<TaskWorkPlanDetail> taskWorkPlanDetails = taskWorkPlanDetailMapper.selectList(workPlanDetailWrapper);
                for (TaskWorkPlanDetail taskWorkPlanDetail : taskWorkPlanDetails) {
                    taskWorkPlanDetail.setState(2);
                    taskWorkPlanDetailMapper.updateById(taskWorkPlanDetail);
                }
                taskWorkPlan.setApprover(token.getUser().getId().toString());
                taskWorkPlan.setApproveTime(new Date());
            }
            if (taskWorkPlan.getState()==1){
                Wrapper<TaskWorkPlanDetail> workPlanDetailWrapper=new EntityWrapper<>();
                workPlanDetailWrapper.eq("pid",taskWorkPlan.getId());
                List<TaskWorkPlanDetail> taskWorkPlanDetails = taskWorkPlanDetailMapper.selectList(workPlanDetailWrapper);
                for (TaskWorkPlanDetail taskWorkPlanDetail : taskWorkPlanDetails) {
                       if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlanDetail.getState(),null)){
                            if (taskWorkPlanDetail.getState()!=2){
                                taskWorkPlanDetail.setState(1);
                            }
                       }else {
                           taskWorkPlanDetail.setState(1);
                       }
                    taskWorkPlanDetailMapper.updateById(taskWorkPlanDetail);
                }
            }
            if (taskWorkPlan.getState()==3){
                taskWorkPlan.setApprover(token.getUser().getId().toString());
                taskWorkPlan.setApproveTime(new Date());
            }
        }

        boolean b = super.updateById(taskWorkPlan);
        return b;
    }
    @Override
    public boolean deleteTaskWorkPlan(Long id, Token token) {
         Boolean result=false;
         //自己创建自己可删除
        TaskWorkPlan taskWorkPlan = super.selectById(id);
        if (taskWorkPlan.getCreator().equals(token.getUser().getId().toString())){
            result = super.deleteById(id);
            Wrapper<TaskWorkPlanDetail> workPlanDetailWrapper=new EntityWrapper<>();
            workPlanDetailWrapper.eq("pid",id);
            taskWorkPlanDetailMapper.delete(workPlanDetailWrapper);
        }
        return result;
    }

    @Override
    public Page<TaskWorkPlan> select(QueryTaskWorkPlan queryTaskWorkPlan,Token token) {

        Page<TaskWorkPlan> page =new Page<>(queryTaskWorkPlan.getCurrentPage(),queryTaskWorkPlan.getPageSize());
         Wrapper<TaskWorkPlan> wrapper=new EntityWrapper<>();
         if (AdscmBaseUtils.getInstance().isNotEmpty(queryTaskWorkPlan.getCreator(),null)){
             wrapper.eq("creator",queryTaskWorkPlan.getCreator());
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(queryTaskWorkPlan.getState(),null)){
             wrapper.eq("state",queryTaskWorkPlan.getState());
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(queryTaskWorkPlan.getPlanStartTime(),null)){
             wrapper.ge("plan_start_time",queryTaskWorkPlan.getPlanStartTime());
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(queryTaskWorkPlan.getPlanEndTime(),null)){
             wrapper.le("plan_end_time",queryTaskWorkPlan.getPlanEndTime());
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(queryTaskWorkPlan.getDeptId(),null)){
             wrapper.eq("dept",queryTaskWorkPlan.getDeptId());
         }

        page= super.selectPage(page,wrapper);
        List<TaskWorkPlan> records = page.getRecords();
        for (TaskWorkPlan record : records) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(record.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(record.getCreator());
                record.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(record.getApprover(),null)){
                JyUser jyUser = jyUserMapper.selectById(record.getApprover());
                record.setApproverName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(record.getDept(),null)){
                JyDept jyDept = jyDeptMapper.selectById(record.getDept());
                record.setJyDept(jyDept);
            }

        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public Page<TaskWorkPlan> selectCc(QueryTaskWorkPlan queryTaskWorkPlan, Token token) {
        Page<TaskWorkPlan> page =new Page<>(queryTaskWorkPlan.getCurrentPage(),queryTaskWorkPlan.getPageSize());
        queryTaskWorkPlan.setCreator(token.getUser().getId().toString());
        queryTaskWorkPlan.setCurrentPage(queryTaskWorkPlan.getCurrentPage()*(queryTaskWorkPlan.getCurrentPage()-1));

        List<TaskWorkPlan>   taskWorkPlans = taskWorkPlanMapper.selectCc(queryTaskWorkPlan);
        Integer  counts=taskWorkPlanMapper.selectCcCounts(queryTaskWorkPlan);
        for (TaskWorkPlan taskWorkPlan : taskWorkPlans) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlan.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(taskWorkPlan.getCreator());
                taskWorkPlan.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlan.getApprover(),null)){
                JyUser jyUser = jyUserMapper.selectById(taskWorkPlan.getApprover());
                taskWorkPlan.setApproverName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlan.getDept(),null)){
                JyDept jyDept = jyDeptMapper.selectById(taskWorkPlan.getDept());
                taskWorkPlan.setJyDept(jyDept);
            }
        }
        page.setRecords(taskWorkPlans);
        page.setTotal(counts);

        return page;
    }
}
